#include <REGX52.H>
#include "Key.H"
#include "Timer0.H"
#include "Nixie.H"

sbit Motor=P1^0;//定义电机的端口

unsigned char Count,Compare;	

void main ()
{
	unsigned char KeyNum,Speed;
	Nixie(1,0);			

	Timer0_Init();
	while(1)
	{
		
		KeyNum=Key();//读取按键的键码
		
		
		if(KeyNum==1)//一级调速
		{	
			Speed=0;
			Nixie(1,0);
		}
		if(KeyNum==2)//二级调速
		{	
			Speed=1;
			Nixie(1,1);
		}		
		if(KeyNum==3)//三级调速
		{	
			Speed=2;
			Nixie(1,2);
		}
		if(KeyNum==4)//四级调速
		{	
			Speed=3;
			Nixie(1,3);
		}
		
		switch (Speed)//条件判断，用于切换四种模式
		{
			case 0: Compare=0;break;
			case 1: Compare=50;	break;		
			case 2: Compare=75;break;
			case 3: Compare=100;break;

		}
	}
}

void Timer0_Routine() interrupt 1 //中断函数，配置10ms自增一次
{	
	TL0 = 0x9C;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	Count++;
	Count%=100;	//如果Count为1,2,3..99时余数为1,2,3..99。当Count为100时，余数为0.（可实现Count大于100时清零，类似IF的条件判断）
	if(Count<Compare)  //判断何时调整占空比
	{
		Motor=1;
	}
	else
	{
		Motor=0;
	}
	
	
	
}
